package com.diaohw.module.file.service.impl;

import com.diaohw.module.file.mapper.FileTagMapper;
import com.diaohw.module.file.mapper.TagMapper;
import com.diaohw.module.file.obj.dto.TagDTO;
import com.diaohw.module.file.obj.entity.TagDO;
import com.diaohw.module.file.obj.query.TagsQuery;
import com.diaohw.module.file.obj.tables.FileTagDef;
import com.diaohw.module.file.obj.vo.TagVO;
import com.diaohw.module.file.service.FileTagService;
import com.diaohw.module.file.service.TagService;
import com.diaohw.platform.common.obj.page.PageResultVo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 标签 服务实现类
 * </p>
 *
 * @author D-Hw
 * @since 2024-09-20
 */
@Service
public class TagImpl implements TagService {

    @Resource
    private FileTagMapper fileTagMapper;

    @Resource
    private TagMapper tagMapper;
    @Resource
    private FileTagService fileTagService;

    @Override
    public TagVO create(TagDTO data) {
        TagDO model = INSTANCE.dtoToEntity(data);
        tagMapper.insert(model);
        return INSTANCE.entityToVo(model);
    }

    @Override
    public TagVO update(String id, TagDTO data) {
        TagDO model = INSTANCE.dtoToEntity(data);
        model.setId(id);
        tagMapper.update(model);
        return INSTANCE.entityToVo(model);
    }

    @Override
    public Boolean delete(String id) {
        fileTagService.delete(FileTagDef.FILE_TAGS.TAG_ID, List.of(id));
        return tagMapper.deleteById(id) > 0;
    }

    @Override
    public TagVO detail(String id) {
        TagDO model = tagMapper.selectOneById(id);
        return INSTANCE.entityToVo(model);
    }

    @Override
    public List<TagVO> findByList(TagsQuery query) {
        QueryWrapper wrapper = TagMapper.createWrapper(query);
        List<TagDO> list = tagMapper.selectListByQuery(wrapper);
        return INSTANCE.entityToVo(list);
    }

    @Override
    public PageResultVo<TagVO> findByPage(TagsQuery query) {
        QueryWrapper wrapper = TagMapper.createWrapper(query);

        Page<TagDO> page = tagMapper.paginate(query.getPageIndex(), query.getPageSize(), wrapper);
        List<TagVO> list = INSTANCE.entityToVo(page.getRecords());
        return PageResultVo.create(page.getTotalRow(), list);
    }

}
